﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownFileSplitter.Htm
{
    internal class ScriptUtils
    {
        /// <summary>
        /// 获取选中的文字
        /// </summary>
        /// <returns></returns>
        public static string GetScript_GetSelection()
        {
            return @"(function() {
                        var selectedText = '';
                        var selectionRange = null;
                        var startOffset = -1;
                        var endOffset = -1;
                        var selection = window.getSelection();
                        if (selection.rangeCount > 0) {
                            selectionRange = selection.getRangeAt(0);
                            selectedText = selection.toString();
                            startOffset = selectionRange.startOffset;
                            endOffset = selectionRange.endOffset;
                        }
                        var range = selectionRange.cloneRange();
                        range.selectNodeContents(document.body);
                        var preSelectionRange = range.cloneRange();
                        preSelectionRange.setEnd(selectionRange.startContainer, selectionRange.startOffset);
                        var start = preSelectionRange.toString().length;
                        var end = start + selectedText.length;

                        var jsonObject = {
                            Text: selectedText,
                            StartIndex: start,
                            EndIndex: end
                        };
                        // 将json对象转换为字符串
                        return JSON.stringify(jsonObject);
                    })();";
        }
        /// <summary>
        /// 设置为正文
        /// </summary>
        /// <returns></returns>
        public static string GetScript_SetAsNormalText()
        {
            return @"(function() {
                        if (window.getSelection) {
                            // 现代浏览器方式
                            var selection = window.getSelection();
                            if (selection.rangeCount > 0) {
                                var range = selection.getRangeAt(0);
                                var selectedText = range.toString();
                                var newNode = document.createElement('p');
                                newNode.innerHTML = selectedText;
                                range.deleteContents();
                                range.insertNode(newNode);
                                return true;
                            }
                        }
                        return false;
                    })();";
        }

        /// <summary>
        /// 设置为标题
        /// </summary>
        /// <param name="headingLevel"></param>
        /// <returns></returns>
        public static string GetScript_SetAsHeading(int headingLevel)
        {
            return @"(function() {
                        if (window.getSelection) {
                            // 现代浏览器方式
                            var selection = window.getSelection();
                            if (selection.rangeCount > 0) {
                                var range = selection.getRangeAt(0);
                                var selectedText = range.toString();
                                var newNode = document.createElement('h" + headingLevel + @"');
                                newNode.innerHTML = selectedText;
                                range.deleteContents();
                                range.insertNode(newNode);
                                return true;
                            }
                        }
                        return false;
                    })();"; ;
        }
    }
}
